﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using PSTUTemplate.Models.Object;
using PSTUTemplate.Models.DBObject;
using PSTUTemplate.Models;
using PSTUTemplate.Models.Object.ViewModels;

namespace PSTUTemplate.Controllers
{

    public class EnrolmentController : Controller
    {
        Repository rep = new Repository();
        //
        // GET: /Enrolment/

        public ActionResult Index()
        {
            EnrolMentCreateModel profileViewModel = new EnrolMentCreateModel();

            profileViewModel.EnrolList = rep.GetStudentList().ToList();
            profileViewModel.Enrols.StudentEnrols = new StudentEnrol();
            return View(profileViewModel);
        }

        //
        // GET: /Enrolment/Details/5
        public ActionResult Details(int id)
        {
            //string loggeduser = User.Identity.Name;
            EnrolMentCreateModel viewModels = new EnrolMentCreateModel();
            //viewModels.Enrols = new Enrol();
            viewModels.Enrols.StudentEnrols = rep.GetAStudentById(id);
            return View(viewModels);
        }

        //
        // GET: /Enrolment/Edit/5
        public ActionResult Edit(int id)
        {
            //string loggeduser = User.Identity.Name;
            StudentEnrol viewModels = new StudentEnrol();
            //viewModels.Enrols = new Enrol();
            viewModels = rep.GetAStudentById(id);
            return View(viewModels);
        }
        //
        // GET: /Enrolment/Edit/5
        [HttpPost]
        public ActionResult Edit(StudentEnrol aStudent)
        {
            //string loggeduser = User.Identity.Name;

            //viewModels.Enrols = new Enrol();
            StudentEnrol viewModels = rep.GetAStudentById(aStudent.Id);

            viewModels.Amount = aStudent.Amount;
            viewModels.BankReceiptNo = aStudent.BankReceiptNo;
            viewModels.ClassStartDate = aStudent.ClassStartDate;
            viewModels.EnrolmentDate = aStudent.EnrolmentDate;
            rep.Save();

            return RedirectToAction("Details", "Enrolment", new { id = viewModels.Id });


        }
        private void BindCourseGridList(EnrolMentCreateModel viewModels)
        {
            //Repository rep = new Repository();
            IQueryable<Course> CourseList = rep.GetCourseListBylevelAndSemester(viewModels.Profiles.Level, viewModels.Profiles.Semester);
            viewModels.Enrols.GridCourseList = CourseList;


            List<SelectListItem> listItems = new List<SelectListItem>();

            SelectListItem list = new SelectListItem();
            list.Value = "0";
            list.Text = "Select FI";

            listItems.Add(list);

            foreach (var item in CourseList)
            {
                list = new SelectListItem();

                list.Value = item.Id.ToString();
                list.Text = item.Title;

                listItems.Add(list);

            }
            viewModels.Enrols.DropDownCourse = listItems;
        }

        private List<SelectListItem> BindDropdownList()
        {
            Repository rep = new Repository();
            IQueryable<Course> CourseList = rep.GetCourseList();
            List<SelectListItem> listItems = new List<SelectListItem>();

            SelectListItem list = new SelectListItem();
            list.Value = "0";
            list.Text = "Select FI";

            listItems.Add(list);

            foreach (var item in CourseList)
            {
                list = new SelectListItem();

                list.Value = item.Id.ToString();
                list.Text = item.Title;

                listItems.Add(list);

            }
            return listItems;
        }

        //
        // GET: /Enrolment/Create
        //[Authorize(Roles = "User")]
        [Authorize]
        public ActionResult Create()
        {
            string loggeduser = User.Identity.Name;

            EnrolMentCreateModel viewModels = new EnrolMentCreateModel();

            viewModels.Enrols = new Enrol();

            viewModels.Profiles = rep.GetProfileByLoggedUserName(loggeduser);

            BindCourseGridList(viewModels);

            return View(viewModels);
        }

        //
        // POST: /Enrolment/Create        
        //[Authorize(Roles = "User")] 
        [HttpPost]
        public ActionResult Create(EnrolMentCreateModel viewModels)
        {
            Repository rep = new Repository();
            //StudentEnrol newStudent = viewModels.Enrols.Enrols;

            if (string.IsNullOrEmpty(viewModels.Enrols.StudentEnrols.StudentStatus))
            {
                ModelState.AddModelError("viewModels.Enrols.Enrols.StudentStatus", "Student Status Cannot be empty!");
            }
            if (ModelState.IsValid)
            {
                int sId = rep.AddStudent(viewModels.Enrols.StudentEnrols);


                FIRemoval fi = null;
                if (viewModels.Enrols.FI1 > 0)
                {
                    fi = new FIRemoval();
                    fi.CourseId = viewModels.Enrols.FI1;
                    fi.StudentId = sId;
                    rep.AdDFIRemoval(fi);
                }
                if (viewModels.Enrols.FI2 > 0)
                {
                    fi = new FIRemoval();
                    fi.CourseId = viewModels.Enrols.FI2;
                    fi.StudentId = sId;
                    rep.AdDFIRemoval(fi);
                }

                if (viewModels.Enrols.FI3 > 0)
                {
                    fi = new FIRemoval();
                    fi.CourseId = viewModels.Enrols.FI3;
                    fi.StudentId = sId;
                    rep.AdDFIRemoval(fi);
                }

                if (viewModels.Enrols.FI4 > 0)
                {
                    fi = new FIRemoval();
                    fi.CourseId = viewModels.Enrols.FI4;
                    fi.StudentId = sId;
                    rep.AdDFIRemoval(fi);
                }

                return RedirectToAction("Details", "Enrolment", new { id = sId });
            }

            string loggeduser = User.Identity.Name;


            viewModels.Profiles = rep.GetProfileByLoggedUserName(loggeduser);
            BindCourseGridList(viewModels);

            return View(viewModels);
        }

    }
}
